package wec_wallet.ebo.com.activity;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.MyBaseActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import wec_wallet.ebo.com.utils.URLs;
import bassproject.ebo.com.ebobass.utils.CacheUtility;
import bassproject.ebo.com.ebobass.utils.IDeviceId;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.JsonT;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.bean.UserInfo;
import wec_wallet.ebo.com.common.Constants;
import wec_wallet.ebo.com.utils.Functions;
import wec_wallet.ebo.com.widget.EaseSwitchButton;

public class SerialNumberActivity extends MyBaseActivity {

    private TextView serial_text_mac, serial_switch_text;
    private EaseSwitchButton serial_switch_btn;
    IHttpPostListener postListener = new IHttpPostListener() {
        @Override
        public void onTimeOut() {

        }

        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            if (!TextUtils.isEmpty(result)) {
                JsonT json = JSON.parseObject(result, new TypeReference<JsonT<String>>() {
                });
                if (json.getIs_auth() != null && json.getIs_auth() == 0) {
                    Functions.relogin(SerialNumberActivity.this);
                } else {
                    if (json.getError() == 1) {
                        show_Toast("返回结果：" + json.getError_msg());
                    } else if (json.getError() == 0) {
                        if (serial_switch_btn.isSwitchOpen()) {
                            serial_switch_btn.closeSwitch();
                            serial_switch_text.setText(getString(R.string.switch_close));
                            if (I_Share.isLogin()) {
                                CacheUtility.spSave(SerialNumberActivity.this, Constants.SERIALNUM_OPEN + I_Share.readAccountUserid(), false);
                            } else {
                                Functions.relogin(SerialNumberActivity.this);
                            }
                        } else {
                            serial_switch_btn.openSwitch();
                            serial_switch_text.setText(getString(R.string.switch_open));
                            if (I_Share.isLogin()) {
                                CacheUtility.spSave(SerialNumberActivity.this, Constants.SERIALNUM_OPEN + I_Share.readAccountUserid(), true);
                            } else {
                                Functions.relogin(SerialNumberActivity.this);
                            }
                        }
                    }
                }
            }
        }
    };
    IHttpPostListener postListener1 = new IHttpPostListener() {
        @Override
        public void onTimeOut() {

        }

        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            if (!TextUtils.isEmpty(result)) {
                UserInfo json = JSON.parseObject(result, UserInfo.class);
                if (json.getIs_auth() != null && json.getIs_auth() == 0) {
                    Functions.relogin(SerialNumberActivity.this);
                } else {
                    if (json.getError() == 1) {

                    } else if (json.getError() == 0) {
                        if (TextUtils.isEmpty(json.getEquipment_id())) {
                            serial_switch_btn.closeSwitch();
                            serial_switch_text.setText(getString(R.string.switch_close));
                        } else {
                            serial_switch_btn.openSwitch();
                            serial_switch_text.setText(getString(R.string.switch_open));

                        }
                    }
                }
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serial_number);
        initViews();


    }

    @Override
    public void initViews() {
        serial_text_mac = (TextView) findViewById(R.id.serial_text_mac);
        serial_switch_text = (TextView) findViewById(R.id.serial_switch_text);
        serial_switch_btn = (EaseSwitchButton) findViewById(R.id.serial_switch_btn);
        serial_switch_btn.setOnClickListener(this);
        serial_text_mac.setText(getResources().getString(R.string.SerialNumber_MAC) + ":" + IDeviceId.getDeviceID());
        findViewById(R.id.actionbar_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.serial_switch_btn:
                toggleBlockGroup();
                break;
        }
    }

    private void getInfo() {
        if (I_Share.isLogin()) {
            Map<String, Object> postParams = new HashMap<String, Object>();
            postParams.put("token", I_Share.readAccountToken());
            postParams.put("uid", I_Share.readAccountUserid());

//            postParams.put("lan", I_Share.getLanguageType_String());
            Functions.post(this, URLs.User_Info, postParams, postListener1, true, true);
        } else {
            finish();
        }


    }

    private void toggleBlockGroup() {
        if (I_Share.isLogin()) {
            Map<String, Object> postParams = new HashMap<String, Object>();
            postParams.put("equipment_id", IDeviceId.getDeviceID());
            postParams.put("token", I_Share.readAccountToken());
            postParams.put("uid", I_Share.readAccountUserid());
            Functions.post(this, URLs.Mobile_bind, postParams, postListener, true, true);
        } else {
            Functions.relogin(this);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 200:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

//                    serial_text_mac.setText(getResources().getString(R.string.SerialNumber_MAC) + ":" + SerialClass.getMac(this));

                } else {
                    IToast.show("请打开查询权限");
                }
                break;
        }
    }


}
